﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Q.Framework.WebControls;
using System.Web.UI.WebControls;
using Q.Framework.Utility;
using System.Web.UI;
using System.Web;

namespace Q.Framework.WebCode
{
    abstract public class DropDownListFastCrud : System.Web.UI.UserControl
    {
        abstract public void OnSelect();
        abstract public QDropDownList GetKeyFieldControl();
        abstract public ImageButton GetOpenCrudButton();
        abstract public AjaxControlToolkit.ModalPopupExtender GetCrudModal();
        abstract public System.Web.UI.HtmlControls.HtmlGenericControl GetCrudIframe();
        abstract public Button GetOkCrudButton();
        abstract public HiddenField GetHiddenField();

        public ILookupSearch LookupDefinition
        {
            get { return ViewState["LookupDefinition_" + ID] as ILookupSearch; }
            set { ViewState["LookupDefinition_" + ID] = value; }
        }

        public Boolean IsValidValue
        {
            get { return Convert.ToBoolean(ViewState["LookupIsValidValue_" + ID] as String); }
            set { ViewState["LookupIsValidValue_" + ID] = value.ToString(); }
        }

        private Object selectedValue;
        public Object SelectedValue
        {
            get { return selectedValue; }
            set
            {
                selectedValue = value;
                if (ExternChange)
                {
                    GetKeyFieldControl().SetValue(GetKeyValueFromObject(value));
                }
            }
        }

        private Object GetKeyValueFromObject(Object obj)
        {
            if (obj == null)
                return String.Empty;

            return LookupDefinition.BusinessType.GetProperty(LookupDefinition.KeyFieldName).GetValue(obj, null);
        }

        private Boolean ExternChange = true;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            GetKeyFieldControl().SelectedIndexChanged += new EventHandler(DropDownListFastCrud_SelectedIndexChanged);
            GetOpenCrudButton().Click += new ImageClickEventHandler(OpenCrudButton_Click);
            GetOkCrudButton().Click += new EventHandler(OkCrudButton_Click);
            if (!IsPostBack)
            {
                FillDropDown();
            }
        }

        private void FillDropDown()
        {
            GetKeyFieldControl().Clear();
            GetKeyFieldControl().DataValueField = LookupDefinition.KeyFieldName;
            GetKeyFieldControl().DataTextField = LookupDefinition.DescriptionFieldName;
            GetKeyFieldControl().DataSource = LookupDefinition.GetPagedList(String.Empty, 0, 999999);
            GetKeyFieldControl().DataBind();
            GetKeyFieldControl().Items.Insert(0, new ListItem("--Selecione--", ""));
        }

        void DropDownListFastCrud_SelectedIndexChanged(object sender, EventArgs e)
        {
            ExternChange = false;
            if (GetKeyFieldControl().SelectedIndex > 0)
            {
                SelectedValue = LookupDefinition.GetObjectByKeyField(GetKeyFieldControl().SelectedValue);
                IsValidValue = true;
            }
            else
            {
                SelectedValue = null;
                IsValidValue = false;
            }

        }

        protected void OkCrudButton_Click(object sender, EventArgs e)
        {
            ExternChange = false;
            SelectedValue = LookupDefinition.GetObjectByKeyField(GetHiddenField().Value);
            FillDropDown();
            GetKeyFieldControl().SelectedValue = GetHiddenField().Value;
            IsValidValue = true;
        }

        protected void OpenCrudButton_Click(object sender, ImageClickEventArgs e)
        {
            LookupDefinition.CrudParameters.Add("ClientOkButtonID", GetOkCrudButton().ClientID);
            LookupDefinition.CrudParameters.Add("FastCrud", "S");
            LookupDefinition.CrudParameters.Add("ClientKeyFieldID", GetHiddenField().ClientID);
            LookupDefinition.CrudParameters.Add("KeyFieldFastCrud", LookupDefinition.KeyFieldName);

            GetCrudIframe().Attributes["src"] = VirtualPathUtility.ToAbsolute(LookupDefinition.CrudUrl) + "?" + String.Join("&", LookupDefinition.CrudParameters.Select(p => p.Key + "=" + p.Value));

            GetCrudModal().Show();
        }

        public void Clear()
        {

        }

        public T Get<T>()
        {
            if (IsValidValue)
                return (T)(SelectedValue = SelectedValue ?? LookupDefinition.GetObjectByKeyField(GetKeyFieldControl().Text));
            else
                return default(T);
        }

        public void Fill<T>(LookupSearch<T> definition)
        {
            LookupDefinition = definition;
        }
    }
}
